<?php
/**
 * Created by Jayli.
 * User: Administrator
 * Date: 2018-03-07
 * Time: 14:46
 */

namespace App\Http\Controllers\Web\Member\Buyer;

use App\Http\Controllers\Web\BaseController;
use App\Services\Cookie\Carts;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;

class OrderAttributeController extends BaseController
{
    protected $api_url = [
        'store' => ['post', 'buyer/attribute'],
        'invoices_index' => ['get', 'buyer/invoices'],
        'attri_order' => ['get', 'buyer/skus/attribute'],
    ];

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function create()
    {
        if(session('attri_arr')){
            $arri_arr=session('attri_arr');
            $arr=unserialize($arri_arr);
            if(!empty($arr['attris'][0])){
                foreach($arr['attris'][0] as $k=>$v){
                    $arr_attris=explode(';',$v['attributes']);
                    foreach($arr_attris as $key=>$val){
                        $arr2=explode(',',$val);
                        $arr_attris[$key]=$arr2;
                        $text='';
                        foreach($arr2 as $key2=>$val2){
                            $arr3=explode(':',$val2);
                            $arr_attris[$key][$key2]=$arr3;
                            if($key>0){
                                $text .= $arr3[1] . '：';
                                $arr['attris'][0][$k]['attrs']['text'][$key]=rtrim($text, '：'); ;

                            }
                        }
                    }
                    $arr['attris'][0][$k]['attrs']['level']=isset($arr_attris[0][1][1])?$arr_attris[0][1][1]:'';
                }
            }
        }else{
            $arr=[];
            return redirect()->back();
        }
        //请求部分-发票
          $invoices = http_web($this->api_url['invoices_index'])->send(['type' => 'all']);

          $default_invoice = null;
          foreach($invoices['data'] as $invoice){
              if($invoice['is_default'] == 1){
                  $default_invoice = $invoice;
              }
          }
          $ret = [
              'shops' =>$arr,
              'default_invoice' => $default_invoice
          ];
//        dd($ret);
        return view('web.order.cart_service',$ret);
    }

    public function store(Carts $carts_service)
    {
        $this->validate($this->request, [
            'shop_id' => 'required',
            'shop_name' => 'required',
            'attri' => 'required|array',
            'invoices' => 'required|array',
        ]);
        $data=$this->request->all();
        if(isset($data['enclosure'])){
            $data['appendix']=json_encode($data['enclosure']);
        }else{
            $data['appendix']=json_encode([]);
        }

        //请求部分
        $ret = http_web($this->api_url['store'])->send($data);

        if($ret['code'] == 0){
            return response()->json(['code'=>0,'message'=>'订单生成成功','data'=>$ret['data']['data']]);
        }else{
            return response()->json(['code'=>1,'message'=>'订单生成失败']);
        }

    }

    public function add_arris(){
        $this->validate($this->request, [
            'ids' => 'required|array',
            'shop_id'=>'required'
        ],[
            'ids.required'=>'没有选择任何商品'
        ]);
        $data = $this->request->all(['ids','shop_id']);

//        //请求部分-直接下单
        $skus = http_web($this->api_url['attri_order'])->send($data);

        //存在自己店铺产品
        if($skus['code'] == 1244){
            return response()->json(['code'=>1,'message'=>'不能购买自己的产品']);
        }

        redirect('/member/buyer/attribute/create')->with('attri_arr',serialize($skus['data'][0]));
            return response()->json(['code'=>0,'message'=>'不能购买自己的产品']);

    }


}